/*
*    Qizx/open 3.0 beta
*
* This code is the open-source version of Qizx.
* Copyright (C) 2004-2009 Axyana Software -- All rights reserved.
*
* The contents of this file are subject to the Mozilla Public License 
*  Version 1.1 (the "License"); you may not use this file except in 
*  compliance with the License. You may obtain a copy of the License at
*  http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
*  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
*  for the specific language governing rights and limitations under the
*  License.
*
* The Initial Developer of the Original Code is Xavier Franc - Axyana Software.
*
*/
using System;
using System.Collections.Generic;
using DataModelException = System.Qizx.Api.DataModelException;
using Node = System.Qizx.Api.Node;
using QName = System.Qizx.Api.QName;
using XMLPullStream = System.Qizx.Api.XMLPullStream;
using Selection = System.Qizx.Queries.Selection;
namespace System.Qizx.Xdm
{
	
	/// <summary> Scans a XML tree and "highlights" fragments of text matching the words or
	/// phrases of a full-text query.
	/// <p>
	/// Being defined as an implementation of XMLPullStream, The "highlighting"
	/// process actually consists of detecting fragments of text matched by the
	/// full-text query; a method {@link #getFragmentStyle()} return an integer
	/// "style" which is the rank of the matched basic full-text expression (simple
	/// word or phrase).
	/// <p>
	/// Optionally the highlighting process can be restricted to a set of nodes
	/// (called "roots") within the main tree. Those nodes are typically the nodes
	/// matching (or returned by) the full-text query.
	/// </summary>
	public class FullTextHighlighter:XMLPullStreamBase
	{
		virtual public Selection Query
		{
			set
			{
				// TODO
			}
			
		}
		/// <summary> Returns the "highlight style" associated with a text fragment.
		/// <p>
		/// A fragment matching the N-th elementary query (simple word or phrase)
		/// returns a value equal to N (starting from 0). A plain fragment of text
		/// (non highlighted) returns -1;
		/// </summary>
		virtual public int FragmentStyle
		{
			get
			{
				return - 1;
			}
			
		}
		public override QName Name
		{
			get
			{
				return null; // TODO
			}
			
		}
		private HashSet<Node> roots;
		private Node mainNode;
		private XMLPullStream source;
		
		/*
		* Implem: le + simple semble un buffer glissant qui contienne des evenements
		* et les donnees associees (name, value, attr)
		* Les mots sont considérés comme evts.
		* Deux modes: 1) hors roots, replication simple de la source
		* 2) tokenisation: tirer N evts (N ~= 1000) dans le buffer, puis rechercher
		* des occurrences avec des iterators semblables a ceux de TokenStream
		* Noter les positions dans l'ordre.
		* 3) Generer jusqua qq positions avant la fin du buffer. Quand un evt mot
		* correspond au premier iterator, flusher le texte, generer fragment
		* puis avancer iterator (et nvelle recherche premier iter)
		*/
		
		public FullTextHighlighter(Node mainNode)
		{
			this.mainNode = mainNode;
		}
		
		public virtual void  addRoot(Node root)
		{
			if (roots == null)
			{
				roots = new HashSet<Node>();
			}
			roots.Add(root);
		}
		
		public override int moveToNextEvent()
		{
			int evt = source.moveToNextEvent();
			switch (evt)
			{
				
				case System.Qizx.Api.XMLPullStream_Fields.TEXT: 
					break;
				}
			return evt;
		}
	}
}